package ar.edu.unlam.tpfinal.struts.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import ar.edu.unlam.tpfinal.service.TransactionTypeService;
import ar.edu.unlam.tpfinal.struts.forms.TransactionTypeForm;

public class TransactionTypeEditAction extends Action {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
								 HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		TransactionTypeForm transactionTypeFormForEdit = new TransactionTypeForm();
		if(form != null && ((TransactionTypeForm) form).getOid()!=null && ((TransactionTypeForm) form).getOid().longValue() != 0){
			TransactionTypeForm transactionTypeForm = (TransactionTypeForm) form;
			WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServlet().getServletContext());
			TransactionTypeService tts = (TransactionTypeService) context.getBean("transactionTypeService");
			
			transactionTypeFormForEdit = tts.getTransactionTypeByOID(transactionTypeForm.getOid());
		}
		request.setAttribute("transactionTypeForm", transactionTypeFormForEdit);
		
		return mapping.findForward("success");
	}
}
